<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>ACE in Action</title>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="../../static/jquery/2.2.4/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <style type="text/css" media="screen">
        #editor {
            width: 100%;
            height: 500px;
            border: none;
        }
        #input {
            width: 65%;
            height: 500px;
            float: left;
        }
        #result {
            width: 35%;
            height: 500px;
            float: right;
        }
        #title {
            text-align:center;
            font-size: large;
        }
        #result-title {
            text-align:center;
        }
        #upload-title {
            text-align:center;
        }
        #input-body ,#title-body, #result-body, #upload-body{
            border: none;
            padding: 0;
        }
        #upload-body{
            width: 100%;
            height: 435px;
        }
        #result-body{
            width: 100%;
            height: 40px;
        }
        #result-txt {
            resize: none;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>

<div class="panel panel-primary">
    <div class="panel-heading">
        <div id="title">
            在线判题
        </div>
    </div>
    <div class="panel-body" id="title-body">
        <div class="panel panel-info" id="input" >
            <div class="panel-heading">
                <div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="myBtn">
                        选择语言 <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="javascript:void(0);" onclick="changeBtn('C')">C</a></li>
                        <li><a href="javascript:void(0);" onclick="changeBtn('Java')">Java</a></li>
                        <li><a href="javascript:void(0);" onclick="changeBtn('Python')">Python</a></li>
                    </ul>
                    <button type="button" class="btn btn-success" onclick="judge()">运行</button>
                </div>
            </div>
            <div class="panel-body" id="input-body">
                <div id="editor"></div>
            </div>
        </div>
        <div class="panel panel-success" id="result">
                <div class="panel-heading">
                    <div id="upload-title">
                        文件上传
                    </div>
                </div>
                <div class="panel-body" id="result-body">

                    <form class="form-inline" id="upload-form" method="post" target="the_iframe">
                        <div class="form-group">
                            <input type="file" id="exampleInputFile" name="file">
                        </div>
                        <button class="btn btn-success" style="float: right" onclick="judgeByFile()">上传并运行</button>
                    </form>
                </div>
            <iframe id="is_iframe" name="the_iframe" style="display:none;"></iframe>
            <div class="panel panel-danger">
                <div class="panel-heading">
                    <div id="result-title">
                        运行结果
                    </div>
                </div>
                <div class="panel-body" id="upload-body">
                    <textarea class="form-control" rows="3" id="result-txt" readonly></textarea>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../src/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/github");
    editor.getSession().setMode("ace/mode/c_cpp");
    document.getElementById('editor').style.fontSize='17px';
    function changeBtn(type) {
        document.getElementById("myBtn").innerHTML=type;
        switch (type) {
            case "C":
                editor.getSession().setMode("ace/mode/c_cpp");
                var editorValue='#include<stdio.h>\n' +
                    'void main(){\n' +
                    '    \n' +
                    '}';
                editor.setValue(editorValue);
                break;
            case "Java":
                editor.getSession().setMode("ace/mode/java");
                var editorValue='// 请不要修改主类名（test），否则会出现问题\n' +
                    'public class test {\n' +
                    '    public static void main(String[] args) {\n' +
                    '    }\n' +
                    '}';
                editor.setValue(editorValue);
                break;
            case "Python":
                editor.getSession().setMode("ace/mode/python");
                var editorValue='';
                editor.setValue(editorValue);
                break;
        }
    }
    function judge() {
        var content = editor.getValue();
        if (content===null||content===""){
            document.getElementById('result-txt').style.color='#FF0000';
            document.getElementById("result-txt").value="运行错误: \n 输入内容不能为空";
            return;
        }
        var form = new FormData;
        var buttonTxt = document.getElementById("myBtn").innerHTML;
        buttonTxt =buttonTxt.toLowerCase();
        if(buttonTxt==="python"){
            buttonTxt="py";
        }
        form.append("content",content);
        form.append("type",buttonTxt);
        $.ajax({
            url: "/judgeByString",
            type: "POST",
            data: form,
            cache: false,
            async: false,
            processData : false,
            contentType : false,
            success: function (data) {
                data = JSON.parse(data);
                var result = data.result;
                if (result==="success"){
                    document.getElementById('result-txt').style.color='#00FF00';
                }else {
                    document.getElementById('result-txt').style.color='#FF0000';
                }
                document.getElementById("result-txt").value=data.data;
                return false;
            },
            error: function () {
                console.log("失败");
            }
        });
    }
    function judgeByFile() {
        var form = new FormData(document.getElementById("upload-form"));
        $.ajax({
            url: "/upload",
            type: "POST",
            data: form,
            cache: false,
            async: false,
            processData: false,
            contentType: false,
            success: function (data) {
                data = JSON.parse(data);
                var result = data.result;
                if (result==="success"){
                    document.getElementById('result-txt').style.color='#00FF00';
                }else {
                    document.getElementById('result-txt').style.color='#FF0000';
                }
                var str = data.data;
                document.getElementById("result-txt").value=str;
            },
            error: function () {
                console.log("失败");
            }
        });
    }
</script>
</body>
</html>